


#include "Snap.h"
#include "assert.h"


Snap::Snap(SpeedMonitor* speedMonitor, bool isFront) : m_speedMonitor(speedMonitor), m_isFront(isFront)
{

		m_indexPre = 0;
		m_indexNow = 0;
		m_counter = 0;
		m_pre = m_snapItemA;
		m_now = m_snapItemB;
}



Snap::~Snap()
{
}

void Snap::reset()
{

		m_indexPre = 0;
		m_indexNow = 0;
		m_counter  = 0;
}


void Snap::update(const SensorState& state)
{
	if(m_counter == 0)
	{

		m_counter ++;
		m_oldState = state;
	}
	else
	{
		assert(m_oldState.state != state.state);
		unsigned long time = state.tick - m_oldState.tick;
		m_now[m_indexNow].type = m_oldState.state;
		m_now[m_indexNow].len = time * m_speedMonitor->speed(); 
		m_indexNow++;
		m_oldState = state;
	}

}


void Snap::clearForNext()
{
	if(m_isFront)
	{
		SnapItem* tmpSi;
		m_indexPre = m_indexNow;
		m_indexNow = 0;
		tmpSi = m_pre;
		m_pre = m_now;
		m_now = tmpSi;

	}
	else
	{
		m_indexNow = 0;
	}
}



const SnapItem* Snap::info(int* cnt)
{
	if(m_isFront)
	{
		*cnt = m_indexPre;
		return m_pre;
	}
	else
	{
		*cnt = m_indexNow;
		return m_now;
	}
}
